############################################
#Project options
TARGET =	CryptoFs
LIBTARGET = libCryptoFs.a
MKFSTARGET = mkfs.crypto
MKFS_OBJS =  mkfs.crypto.o 
#IOStream.o BlockReader.o InodeReader.o Filesystem.o\
	      SuperBlock.o BlockGroup.o Inode.o DirectoryReader.o\
	      DirEntry.o File.o DirectoryReaderLinked.o utils.o\
	      CommandLineParsers.o FSException.o FileContainer.o BlkIndex2NumTranslator.o InodeBlocksAdder.o BlocksNumCache.o


OBJECTS = IOStream.o BlockReader.o InodeReader.o Filesystem.o\
	      CryptoFs.o SuperBlock.o BlockGroup.o Inode.o DirectoryReader.o\
	      DirEntry.o File.o DirectoryReaderLinked.o utils.o\
	      CommandLineParsers.o FileOperations.o DirOperations.o ConfigOperations.o OtherFsOperations.o\
	      FSException.o FileContainer.o BlkIndex2NumTranslator.o InodeBlocksAdder.o BlocksNumCache.o
	      
TESTS = # FileSystemTests #InodeReaderTests BlockReaderTests

TEST_DEPS= 
#$(TARGET).a 
#$(TARGET).a
CUDA_OBJ = Cuda_AES.a
LIBS = -lfuse -lCryptoFs -lcudaaes -lboost_program_options -lssl -lcuda -lcudart
TEST_LIBS = -lcppunit -lssl $(LIBS)

DEFINES=-D_FILE_OFFSET_BITS=64

INCLUDE = ../ ./
LIBDIR = -L/opt/cuda/lib64 -L../Cuda_AES/obj -Lobj

OBJDIR = obj
SRCDIR = src
TSTDIR = test
############################################
#Compilation options


CXX = g++
CC = gcc

CXXFLAGS =-O2 -g -Wall -fmessage-length=0
LDFLAGS=

INCLUDE := $(addprefix -I, $(INCLUDE))
NDEBUG=1
ifdef NDEBUG
CXXFLAGS := -DNDEBUG -O2
NVCCFLAGS := -DNDEBUG -O2
else
CXXFLAGS := -g3 -Wall -pedantic
endif
CXXFLAGS+=$(DEFINES)
############################################
#Build rules

all:  $(TARGET) $(MKFSTARGET) UNIT_TESTS



# $^ skladniki reguly
# $@ regula
# regula: skladniki
$(TARGET): $(OBJDIR)/$(LIBTARGET)
	$(CXX) $(LIBDIR) $(LDFLAGS) $(LIBS) -o $@
	
$(OBJDIR)/$(LIBTARGET): $(addprefix $(OBJDIR)/, $(OBJECTS))
	ar rcs $(OBJDIR)/$(LIBTARGET) $^  

$(MKFSTARGET): $(addprefix $(OBJDIR)/, $(MKFS_OBJS))
	$(CXX) $(LIBDIR) $(LDFLAGS) $^ $(LIBS) -o $@

$(OBJDIR)/%.o : $(SRCDIR)/%.cpp
		$(CXX) $(CXXFLAGS) $(INCLUDE) $< -c -o $@


UNIT_TESTS: $(addprefix $(TSTDIR)/, $(TESTS))
		@cat $(TSTDIR)/runAll.sh | sort | uniq > $(TSTDIR)/runAll2.sh && mv $(TSTDIR)/runAll2.sh $(TSTDIR)/runAll.sh
		@sh $(TSTDIR)/runAll.sh
		


$(TSTDIR)/% : $(TSTDIR)/%.cpp $(TARGET)
		$(CXX) $(CXXFLAGS) $(INCLUDE) $< $(TEST_LIBS) $(addprefix $(OBJDIR)/, $(TEST_DEPS)) $(LIBS) $(LIBDIR)  -o  $@
		@echo $@ >> $(TSTDIR)/runAll.sh
		
clean:
	rm -f $(addprefix $(OBJDIR)/, $(OBJECTS)) $(addprefix $(OBJDIR)/, $(MKFS_OBJS)) $(TARGET)  $(addprefix $(TSTDIR)/, $(TESTS)) $(OBJDIR)/$(TARGET).a test/runAll.sh mkfs.crypto
	rm -f $(OBJDIR)/lib$(TARGET).a 
	touch test/runAll.sh
	@rm /tmp/file*
